import store from "../store"
//请求后端接口的根路径
let baseUrl = "/dpc";
//如果是微信小程序走此代码
// #ifdef MP-WEIXIN || MP-QQ
baseUrl = "http://192.168.31.201:7245"
// #endif	

function CheckToken() {
	if (!store.getters.token) {
		uni.navigateTo({
			url: "/pages/login/login"
		})
		return false;
	}
	return true;
}
/**
 * 获取路径中的文件名称
 */
export function GetFileNameByPath(path) {
	if (!path) {
		return "";
	}
	var pos1 = path.lastIndexOf('/');
	var pos2 = path.lastIndexOf('\\');
	var pos = Math.max(pos1, pos2);
	if (pos < 0) {
		return path;
	} else {
		return path.substring(pos + 1);
	}
}
/**
 * 获取路径中的文件格式
 */
export function GetFileTypeByPath(path) {
	if (!path) {
		return "";
	}
	var pos = path.lastIndexOf('.');
	if (pos < 0) {
		return "";
	} else {
		return path.substring(pos + 1);
	}
}
/**
 * 切割字段返回一个集合
 * @param {*} value 需要切割字段
 * @param {*} cutting 切割的符号
 */
export function ConvertArray(value = '', cutting = ',') {
	if (!value) {
		return [];
	}
	return value.split(cutting);
}


/**
 * 加上本地的地址
 * @param {*} images
 * @returns
 */
export function ReplaceImageHttp(value) {

	if (value.indexOf('http') == -1 && value.indexOf('https') == -1) {
		if (value.indexOf("/") == 0) {
			return baseUrl + value;
		}
		return baseUrl + '/' + value;
	}
	return value;
}
/**
 * 根据路径获取文件的详细信息
 */
export function FullConvertUrlArray(value = '', cutting = ',') {
	var arr = ConvertArray(value, cutting);

	return arr.map(x => {
		return {
			url: ReplaceImageHttp(x),
			name: GetFileNameByPath(x),
			type: GetFileTypeByPath(x)
		};
	});

}
/**
 * 获取对象中指定key的值
 */
export function GetObjectValue(obj, name) {
	if (!name) {
		return undefined;
	}
	if (!obj) {
		return undefined
	};
	if (name.indexOf(".") != -1) {
		var array = name.split(".");

		var value = obj;
		array.forEach((item) => {

			value = value[`${item}`];
		})
		return value;
	} else {
		return obj[`${name}`];
	}
}
/**
 * 根据某个字段进行去重
 */
export function UniqueBy(arr, key) {
	return arr.reduce((acc, cur) => {
		const existing = acc.find(item => item[key] === cur[key]);
		if (!existing) {
			acc.push(cur);
		}
		return acc;
	}, []);
}


function GetFormatFullDate(date) {
	let year = date.getFullYear();
	let month = date.getMonth() + 1;
	let day = date.getDate();
	let hour = date.getHours();
	let minutes = date.getMinutes();
	let seconds = date.getSeconds();
	month = (month < 10) ? '0' + month : month;
	day = (day < 10) ? '0' + day : day;
	hour = (hour < 10) ? '0' + hour : hour;
	minutes = (minutes < 10) ? '0' + minutes : minutes;
	seconds = (seconds < 10) ? '0' + seconds : seconds;
	let currentDate = year + "-" + month + "-" + day +
		" " + hour + ":" + minutes + ":" + seconds;
	return currentDate;
}

function GetFormatHMSDate(date) {
	let year = date.getFullYear();
	let month = date.getMonth() + 1;
	let day = date.getDate();
	let hour = date.getHours();
	let minutes = date.getMinutes();
	let seconds = date.getSeconds();
	month = (month < 10) ? '0' + month : month;
	day = (day < 10) ? '0' + day : day;
	hour = (hour < 10) ? '0' + hour : hour;
	minutes = (minutes < 10) ? '0' + minutes : minutes;
	seconds = (seconds < 10) ? '0' + seconds : seconds;
	let currentDate = hour + ":" + minutes + ":" + seconds;
	return currentDate;
}

function GetFormatShortDate(date) {
	let year = date.getFullYear();
	let month = date.getMonth() + 1;
	let day = date.getDate();
	let hour = date.getHours();
	let minutes = date.getMinutes();
	let seconds = date.getSeconds();
	month = (month < 10) ? '0' + month : month;
	day = (day < 10) ? '0' + day : day;
	hour = (hour < 10) ? '0' + hour : hour;
	minutes = (minutes < 10) ? '0' + minutes : minutes;
	seconds = (seconds < 10) ? '0' + seconds : seconds;
	let currentDate = year + "-" + month + "-" + day;

	return currentDate;
}

function ShowLoading(title = "加载中...") {
	return new Promise((resolve, reject) => {
		uni.showLoading({
			title: title,
			success: resolve(),
			fail: reject()

		})

	})
}

function ShowToast(title = "成功") {
	console.log(title);
	return new Promise((resolve, reject) => {
		uni.showToast({
			icon: "none",
			duration: 3000,
			title: title,

			success: (res) => {
				resolve(res);
			},
			fail: (err) => {
				reject(err);
			}
		})

	})

}


function ShowModal(content = "你确定要操作吗") {

	return new Promise((resolve, reject) => {
		uni.showModal({
			title: "提示",
			content: content,
			success: (res) => {
				console.log(res);
				resolve(res.confirm || false);
			},
			fail: (res) => {
				resolve(false);
			}
		})

	})

}

/**
 * 选择图片
 */
function ChooseImageAsync(count = 1) {
	return new Promise((resolve, reject) => {
		uni.chooseImage({
			count: 4, //默认9
			sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
			sourceType: ['album', "camera"], //从相册选择
			success: (res) => {
				resolve(res);
			}
		});
	})
}
/**
 * 选择视频
 */
function ChooseVideoAsync(count = 1) {
	return new Promise((resolve, reject) => {
		uni.chooseVideo({
			sourceType: ['album', "camera"], //从相册选择
			success: (res) => {
				resolve(res);
			}
		});
	})
}
/**
 * 得到登录的code
 */
function GetLoginCode(count = 1) {
	// #ifdef MP
	return new Promise((resolve, reject) => {
		console.log("aa");
		uni.login({
			"provider": "weixin",
			"onlyAuthorize": true, // 微信登录仅请求授权认证
			success: function(event) {
				console.log("得到登录的code", event.code);
				resolve(event.code);
			},
			fail: function(err) {
				console.log("获取微信的登录状态code失败", err)
				reject(code);
			}
		})


	})
	// #endif

	// #ifdef H5 
	return new Promise((resolve, reject) => {
		console.log("执行")
		resolve(null);
	})
	// #endif

}

function ReturnPage(page = 1, ms = 1000) {
	return new Promise((resolve, reject) => {
		setTimeout(() => {
			let pages = getCurrentPages();
			if (pages.length == 1) {
				uni.redirectTo({
					url: "/pages/index/index"
				})
			} else {
				uni.navigateBack({
					delta: page,
					success: (res) => {
						resolve(res);
					}
				})
			}
		}, ms)

	})
}

function GetTimeDifference(date) {
	const now = new Date();
	const diffInMilliseconds = now - date;
	const diffInSeconds = Math.floor(diffInMilliseconds / 1000);
	const diffInMinutes = Math.floor(diffInSeconds / 60);
	const diffInHours = Math.floor(diffInMinutes / 60);
	const diffInDays = Math.floor(diffInHours / 24);
	const diffInMonths = Math.floor(diffInDays / 30);

	if (diffInMonths > 0) {
		return `${diffInMonths}个月前`;
	} else if (diffInDays > 0) {
		return `${diffInDays}天前`;
	} else if (diffInHours > 0) {
		return `${diffInHours}小时前`;
	} else if (diffInMinutes > 0) {
		return `${diffInMinutes}分钟前`;
	} else {
		return '刚刚发表';
	}
}

/**
 * 获取当前经纬度
 */
function GetCurrentLocation() {
	return new Promise((resolve, reject) => {
		uni.getLocation({
			type: 'gcj02', //返回可以用于uni.openLocation的经纬度
			success: function(res) {
				resolve(res);
				console.log(res, 'res')
			},
			fail: function(f) {
				reject(f)
			}
		});
	})
}
/**
 * 得到地址并写入缓存
 */
async function SyncCurrentLocation() {

	console.log("开始获取当前地址信息")
	let location = null;
	await GetCurrentLocation().then(async res => {
		if (res.latitude && res.longitude) {
			location = {
				latitude: res.latitude,
				longitude: res.longitude,
			}
			await store.dispatch("SyncLocation", location);

		}
	}).catch(f => {
		uni.showToast({
			icon: "none",
			content: "获取地址失败",
		})
	});
	return location;
}
/**
 * 临时文件转换成base64
 * @param {Object} tempFiles
 */
export function TempToBase64(tempFiles) {
	return new Promise((resolve, reject) => {
		uni.getFileSystemManager().readFile({
			filePath: tempFiles,
			encoding: 'base64',
			success(dataRes) {
				console.log('图片的Base64编码结果：' + dataRes.data);
				resolve(dataRes)

				// 这里可以对Base64字符串进行其他操作，比如上传到服务器等
			},
			fail(err) {
				reject(null);
				console.error('读取文件失败：' + err.message);
			}
		});
	})
}



module.exports = {
	TempToBase64,
	GetLoginCode,
	CheckToken,
	ShowLoading,
	ShowToast,
	ShowModal,
	ChooseImageAsync,
	ChooseVideoAsync,
	GetFormatFullDate,
	GetFormatShortDate,
	GetFormatHMSDate,
	ReturnPage,
	ReplaceImageHttp,
	FullConvertUrlArray,
	GetObjectValue,
	UniqueBy,
	GetCurrentLocation,
	SyncCurrentLocation,
	GetTimeDifference
};